package com.jcgyl.export.service.Impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jcgyl.export.dao.TbsysmenuDao;
import com.jcgyl.export.entity.Tbsysmenu;
import com.jcgyl.export.service.PermissionsService;
import com.jcgyl.export.util.entity.Result;

@Service
public class PermissionsServiceImpl implements PermissionsService {
	@Autowired
	private TbsysmenuDao sysmenyDao;

	@Override
	public List<String> findAllPermissions() {
		return sysmenyDao.findAllPermissions();
	}

	@Override
	public List<Integer> findPermissionsbyRId(int rId) {
		return sysmenyDao.findPermissionsbyRId(rId);
	}

	@Override
	public List<Tbsysmenu> findZtreePermissions() {
		return sysmenyDao.findPermissionsZtree();
	}

	@Override
	public Result updatePermissionsbyRoleId(int rid, List<Integer> list) {
		List<Integer> list_q = sysmenyDao.findPermissionsbyRId(rid);
		System.out.println(list_q);
		try {
			int pid;
			for (int i = 0; i < list_q.size(); i++) {
				pid=list_q.get(i);
				sysmenyDao.deletePermissionsbyRoleId(rid,pid);
			}
			for (int i = 0; i < list.size(); i++) {
				pid=list.get(i);
				sysmenyDao.insertPermissionsbyRoleId(rid, pid);
			}
			return Result.ok("保存成功");
		} catch (Exception ex) {
			ex.printStackTrace();
			return Result.build(1, "根据角色ID修改权限时发生错误");
		}
	}

}
